package kr.co.redcircle.loader.data;

import java.util.ArrayList;

import kr.co.redcircle.loader.utility.ByteUtil;


public class DataResultMetaData extends ArrayList<MetaData>{

	private static final long serialVersionUID = 6676874110960793583L;
	
	private int[] fieldLength = null;

	private int metaIndex;

	private byte[] metaRawArray = null;
	
	public DataResultMetaData(int metaDivision) {
		this.metaIndex = metaDivision;
	}

	public int[] getFieldLength() {
		return fieldLength;
	}

	public void setFieldLength(int[] fieldLength) {
		this.fieldLength = fieldLength;
	}

	public int build(byte[] b) {
		if (this.metaRawArray == null){
			
			this.metaIndex += 30;
			byte[] lengthByte = new byte[2];
			System.arraycopy(b, this.metaIndex, lengthByte, 0, 2);
			int metaCount = ByteUtil.byteArrayToInt(lengthByte);
			int[] divisionSectionArray = new int[metaCount];
			
			int i=0;
			this.metaIndex += 96;
			int startIndex = this.metaIndex;
			byte[] divisionSearchArray = new byte[3];
			System.arraycopy(b, this.metaIndex, divisionSearchArray, 0, 3);
			
			// setup byte postion array
			for (i=0; i<metaCount; i++){
				divisionSectionArray[i] = ByteUtil.BytesIndexOf(b, divisionSearchArray, startIndex) + 7;
				startIndex = divisionSectionArray[i];
			}
			
			// setup array copy
			for (i=0; i<metaCount-1; i++){
				int metaByteLength = divisionSectionArray[i+1] - divisionSectionArray[i]; 
				byte[] metaBytes = new byte[metaByteLength];
				System.arraycopy(b, divisionSectionArray[i], metaBytes, 0, metaByteLength);
				MetaData mData = new MetaData(metaBytes);
				mData.load();
				this.add(mData);
			}
			
			return metaCount;
			
		}
		
		return 0;
		
	}
	
	
}
